package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;



    /**
     * 查看购物车菜品数据
     *
     * @return
     */
    @GetMapping("/list")
    public R list() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //获取用户id
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //按照添加时间排序
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        //添加值
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        //返回
        return R.success(list);
    }

    /**
     * 添加数据到购物车中
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        //13812345678

        //获取用户id，设置用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //同一个菜品或者同一个套餐，第一次点是新增，后面点菜时修改（按id）
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //获取 菜品id
        Long dishId = shoppingCart.getDishId();
        //如果菜品 id 不为空就是菜品，如果不是就是套餐
        if (dishId != null) {
            //菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //套餐
            //获取套餐id
            Long setmealId = shoppingCart.getSetmealId();
            wrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }

        //查询一个数据
        ShoppingCart selectOne = shoppingCartService.getOne(wrapper);

        if (selectOne != null) {
            //有数据，更新(数据 加 1)
            selectOne.setNumber(selectOne.getNumber() + 1);
            shoppingCartService.updateById(selectOne);
        } else {
            //没有，则添加
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
            selectOne = shoppingCart;//复制给one中
        }
        return R.success(selectOne);//需要返回购物车数据，前端已定义

    }

    /**
     * 减购物车里数量
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //减一，重新设置
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        //数量大于 0 ，修改值
        if (number > 0) {
            shoppingCartService.updateById(one);
        } else {
            //数量小于 0 ，则删除，然后不能往下执行了
            shoppingCartService.remove(wrapper);
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R cleanShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //删除数据
        shoppingCartService.remove(wrapper);
        return R.success("清空成功");
    }



}